package com.zmj.tl.database.taos.service.impl;

import com.zmj.tl.database.taos.domain.TreatMetric;
import com.zmj.tl.database.taos.domain.TreatSchema;
import com.zmj.tl.database.taos.mapper.TreatExecuteMapper;
import com.zmj.tl.database.taos.service.TreatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author hd
 * @desc
 * @since 2024/2/1 14:25
 */
@Slf4j
@Service("treatService")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class TreatServiceImpl implements TreatService {
    private final TreatExecuteMapper executeMapper;

    /**
     * @param treatSchema 约束
     * @desc 创建库
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public int createDatabase(TreatSchema treatSchema) {
        return this.executeMapper.createDatabase(treatSchema.getDbName());
    }

    /**
     * @param treatSchema 约束
     * @desc 创建超级表
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public int createTable(TreatSchema treatSchema) {
        return this.executeMapper.createTable(treatSchema.getTbName(), treatSchema.getMetricSchema(), treatSchema.getTagSchema());
    }

    /**
     * @param treatSchema 约束
     * @desc 创建子表
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public int createStable(TreatSchema treatSchema) {
        return this.executeMapper.createStable(treatSchema.getStbName(), treatSchema.getTbName(), treatSchema.getTagColumn());
    }

    /**
     * @param treatSchema 约束
     * @desc 删除超级表
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public void dropTable(TreatSchema treatSchema) {
        this.executeMapper.dropTable(treatSchema.getTbName());
    }

    /**
     * @param treatSchema 约束
     * @desc 删除库
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public void dropDatabase(TreatSchema treatSchema) {
        this.executeMapper.dropDatabase(treatSchema.getDbName());
    }

    /**
     * @param metric 表计参数
     * @desc 新增表计参数
     * @author hd
     * @since 2024/2/1 12:34
     **/
    @Override
    public void addOne(TreatMetric metric) {
        TreatSchema treatSchema = new TreatSchema();
        treatSchema.setStbName(metric.getStbName());
        executeMapper.insertOne(treatSchema, metric);
    }

    /**
     * @param metric 表计参数
     * @desc 新增表计参数
     * @author hd
     * @since 2024/2/1 12:35
     **/
    @Override
    public void addEntity(TreatMetric metric) {
        TreatSchema treatSchema = new TreatSchema();
        treatSchema.setStbName(metric.getStbName());
        executeMapper.insertEntity(treatSchema, metric);
    }

    /**
     * @param metric 表计参数
     * @desc 批量新增表计参数
     * @author hd
     * @since 2024/2/1 12:35
     **/
    @Override
    public void addBatch(TreatMetric metric) {
        TreatSchema treatSchema = new TreatSchema();
        executeMapper.insertBatch(treatSchema, metric, metric.getList());
    }
}
